﻿/****************************************************************************************************************
*                                                                                                               *
* Copyright (C) 2011 5173.com                                                                                   *
* This project may be copied only under the terms of the Apache License 2.0.                                    *
* Please visit the project Home Page http://bqqapicsharp.codeplex.com/ for more detail.                         *
*                                                                                                               *
****************************************************************************************************************/

namespace BQQAPIClient.Core.Network
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;
    using BQQAPIClient.Core.Utility;

    internal static class ServerConfigurationProvider
    {
        private static readonly string ServiceConfigFilePath = "config\\BQQAPIClient.config";

        internal static ServerConfiguration GetServerConfigurationFromConfigFile()
        {
            var config = new ServerConfiguration(true, LogLevel.Debug, 100, 100, 34567, 5000, new Queue<Daemon>());
            string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ServiceConfigFilePath);
            if (!File.Exists(configFilePath)) return config;
            XDocument xml = XDocument.Load(configFilePath);

            var serverConfiguration = xml.Descendants("ServerConfiguration").SingleOrDefault();
            if (serverConfiguration != null)
            {
                if (serverConfiguration.Descendants("EnablePerformanceCounter").SingleOrDefault() != null)
                {
                    bool.TryParse(serverConfiguration.Descendants("EnablePerformanceCounter").SingleOrDefault().Value, out config.EnablePerformanceCounter);
                }
                if (serverConfiguration.Descendants("MaxReceiveConnections").SingleOrDefault() != null)
                {
                    int.TryParse(serverConfiguration.Descendants("MaxReceiveConnections").SingleOrDefault().Value, out config.MaxSendConnections);
                }
                if (serverConfiguration.Descendants("MaxSendConnections").SingleOrDefault() != null)
                {
                    int.TryParse(serverConfiguration.Descendants("MaxSendConnections").SingleOrDefault().Value, out config.MaxSendConnections);
                }
                if (serverConfiguration.Descendants("ListeningPort").SingleOrDefault() != null)
                {
                    int.TryParse(serverConfiguration.Descendants("ListeningPort").SingleOrDefault().Value, out config.ListeningPort);
                }
                if (serverConfiguration.Descendants("C2SHeartbeatInterval").SingleOrDefault() != null)
                {
                    int.TryParse(serverConfiguration.Descendants("C2SHeartbeatInterval").SingleOrDefault().Value, out config.C2SHeartbeatInterval);
                }
                if (serverConfiguration.Descendants("LogLevel").SingleOrDefault() != null)
                {
                    if (Enum.IsDefined(typeof(LogLevel), serverConfiguration.Descendants("LogLevel").SingleOrDefault().Value))
                        config.LogLevel = (LogLevel)Enum.Parse(typeof(LogLevel), serverConfiguration.Descendants("LogLevel").SingleOrDefault().Value);
                }
                var daemonList = serverConfiguration.Descendants("DaemonList").SingleOrDefault();
                if (daemonList != null)
                {
                    foreach (var daemon in daemonList.Descendants("Daemon"))
                    {
                        string address = string.Empty;
                        int port = 0;
                        if (daemon.Attribute("Address") != null)
                            address = daemon.Attribute("Address").Value;
                        int.TryParse(daemon.Attribute("Port").Value, out port);
                        if (!string.IsNullOrEmpty(address) && port != 0)
                            config.DaemonList.Enqueue(new Daemon
                            {
                                Address = address,
                                Port = port,
                            });
                    }
                }
                config.DaemonList.Reverse();
            }

            Logger.SetLogLevel(config.LogLevel);
            Logger.Info("Config loaded: " + config.ToString());
            return config;
        }
    }
}
